package com.incar.chuxing.activity.certification;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.activity.MainActivity;
import com.incar.chuxing.activity.WebViewActivity;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.QualificationBean;
import com.incar.chuxing.bean.UserCarInforBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;
import com.incar.chuxing.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 身份认证通过
 */
public class CertifyCardFinishActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_cartype)
    TextView tvCartype;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_starttime)
    TextView tvStarttime;
    @BindView(R.id.tv_endtime)
    TextView tvEndtime;
    @BindView(R.id.tv_intomain)
    TextView tvIntomain;
    private Context mContext = this;
    private String ZhiMaStatus;
    private String deposit;
    private String fromType;


    @Override
    public int getLayoutId() {
        return R.layout.activity_card_finish;
    }

    @OnClick({R.id.tv_intomain})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_intomain:
                if ("1".equals(ZhiMaStatus)) {
                    tvIntomain.setText("立即用车");
                    startActivity(new Intent(mContext, MainActivity.class));
                    AppManager.getAppManager().finishActivity();
                } else if ("1".equals(deposit)) {
                    tvIntomain.setText("立即用车");
                    startActivity(new Intent(mContext, MainActivity.class));
                    AppManager.getAppManager().finishActivity();
                } else {
                    tvIntomain.setText("下一步：信用认证");
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlContainer.Request.h5UrlMap.get(UrlContainer.Request.html_credit_auth));
                    startActivity(intent);
                    AppManager.getAppManager().finishActivity();
                }
                break;
        }
    }

    @Override
    public void initView() {
        fromType = getIntent().getStringExtra("fromType") + "";
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        getUserCarInfo();
        checkUserStatus();
    }

    private void checkUserStatus() {
        showLoadingDialog();
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).getUserStatus(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<QualificationBean>() {
            @Override
            public void onResponse(Call<QualificationBean> call, Response<QualificationBean> response) {
                dismissLoadingDialog();
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        SPUtils.getInstance(mContext).putStringValue(SPKey.REAL_NAME_STATUS, response.body().getAttachment().getIdentityStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.LICENSE_CAR_STATUS, response.body().getAttachment().getLicenseStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.DEPOSIT_CAR_STATUS, response.body().getAttachment().getDepositStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.DEPOSIT_AMOUNT, response.body().getAttachment().getCarBail() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.JISHEN_STATUS, response.body().getAttachment().getStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.ZHIMA_STATUS, response.body().getAttachment().getAuthorize() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.ZHIMA_FEN, response.body().getAttachment().getScore() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.XINYONG_LEVEL, response.body().getAttachment().getLevel() + "");

                        deposit = response.body().getAttachment().getDepositStatus() + "";
                        ZhiMaStatus = response.body().getAttachment().getAuthorize() + "";

                        if ("1".equals(ZhiMaStatus)) {
                            tvIntomain.setText("立即用车");
                            tvIntomain.setVisibility(View.VISIBLE);
                        } else if ("1".equals(deposit)) {
                            tvIntomain.setText("立即用车");
                            tvIntomain.setVisibility(View.VISIBLE);
                        } else if ("2".equals(deposit)) {
                            tvIntomain.setVisibility(View.INVISIBLE);
                        } else {
                            tvIntomain.setText("下一步：信用认证");
                            tvIntomain.setVisibility(View.VISIBLE);
                        }
                    } else {
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                } else {
                    ToastUtils.makeText(mContext, getResources().getString(R.string.serverError), ToastUtils.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<QualificationBean> call, Throwable t) {
                dismissLoadingDialog();
                ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    //认证信息
    private void getUserCarInfo() {
        showLoadingDialog();
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).getUserCarInfo(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<UserCarInforBean>() {
            @Override
            public void onResponse(Call<UserCarInforBean> call, Response<UserCarInforBean> response) {
                dismissLoadingDialog();
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        UserCarInforBean carInforBean = response.body();
                        tvName.setText(carInforBean.getAttachment().getFullName());
                        tvCartype.setText(carInforBean.getAttachment().getQuasiDrivingType());
                        tvStarttime.setText(carInforBean.getAttachment().getLicensingOfTime());
                        tvEndtime.setText(carInforBean.getAttachment().getDrivingLicencePeriodOfValidity());
                    } else {
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                } else {
                    ToastUtils.makeText(mContext, getResources().getString(R.string.serverError), ToastUtils.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserCarInforBean> call, Throwable t) {
                dismissLoadingDialog();
                ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
                Logger.d("neterror", "011" + t);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (fromType.equals("authFlow") || fromType.equals("fromNewUser")) {
            startActivity(new Intent(mContext, MainActivity.class));
            AppManager.getAppManager().finishActivity();
        } else {
            AppManager.getAppManager().finishActivity();
        }
    }
}
